package com.xnyzc.lhy.finance.service.impl.bank;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.*;
import com.xnyzc.lhy.finance.constant.BankCardConstant;
import com.xnyzc.lhy.finance.constant.MessageConstant;
import com.xnyzc.lhy.finance.eenum.ECardType;
import com.xnyzc.lhy.finance.entity.bank.AddBankParams;
import com.xnyzc.lhy.finance.entity.bank.OaDBank;
import com.xnyzc.lhy.finance.entity.bank.OaDBindBankCard;
import com.xnyzc.lhy.finance.mapper.bank.OaDBankMapper;
import com.xnyzc.lhy.finance.mapper.bank.OaDBindBankCardMapper;
import com.xnyzc.lhy.finance.mapper.wallet.OaDWalletMapper;
import com.xnyzc.lhy.finance.params.dto.bank.OaBankCardBindDTO;
import com.xnyzc.lhy.finance.params.vo.bank.DriverBindBankCardVO;
import com.xnyzc.lhy.finance.service.bank.IOaDBankCardService;
import com.xnyzc.lhy.finance.service.fegin.bankcard.FeginCheckBankCardService;
import com.xnyzc.lhy.finance.service.fegin.jpush.FeignJPushService;
import com.xnyzc.lhy.finance.service.fegin.message.FeignMessageService;
import com.xnyzc.lhy.finance.service.fegin.oss.FeginOssService;
import com.xnyzc.lhy.finance.utils.HiddenUtil;
import com.xnyzc.lhy.order.entity.user.message.OaSysMessage;
import com.xnyzc.lhy.resource.entity.jpush.PushInfoEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 司机账单表 服务实现类
 * </p>
 *
 * @author cuixiaojian
 * @since 2019-10-16
 */
@Slf4j
@Service
public class OaDBankCardServiceImpl implements IOaDBankCardService {

    @Autowired
    private OaDBankMapper oaDBankMapper;

    @Autowired
    private OaDWalletMapper oaDWalletMapper;

    @Autowired
    private OaDBindBankCardMapper oaDBindBankCardMapper;

    @Autowired
    private FeginCheckBankCardService feginCheckBankCardService;

    @Autowired
    private FeginOssService feginOssService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private FeignJPushService feignJPushService;

    @Autowired
    private FeignMessageService feignMessageService;

    @Override
    public Rv addBank(AddBankParams params) {
        if (CheckUtil.strIsEmpty(params.getName(), params.getSimp())) {
            return Rv.error(EErrorCode.noData);
        }
        Integer state = params.getState();
        Integer sort = params.getSort();
        if (params.getIcon() == null || state == null || sort == null) {
            return Rv.error(EErrorCode.noData);
        }
        OaDBank bank = new OaDBank();
        bank.setBankCardId(IDUtil.nextId());
        bank.setBank(params.getSimp());
        bank.setIcon(params.getIcon());
        bank.setLabel(params.getName());

        sort = sort > 0 ? sort : 0;
        bank.setSort(sort);

        state = state > 0 ? 1 : 0;//1表示删除
        bank.setDeleted(state);

        int res = oaDBankMapper.insert(bank);
        if (res > 0) {
            return Rv.wrap();
        }
        return Rv.error(EErrorCode.applyAmountOperateFailure);
    }

    @Override
    public Rv updateBank(AddBankParams params) {
        Long id = params.getId();
        if (id == null || id < 0) {
            return Rv.error(EErrorCode.noData);
        }
        OaDBank bank = oaDBankMapper.selectOne(Qw.create().eq(OaDBank.BANK_CARD_ID, id));
        if (bank == null) {
            return Rv.error(EErrorCode.noData);
        }
        if (CheckUtil.strIsNotEmpty(params.getName())) {
            bank.setLabel(params.getName());
        }
        if (CheckUtil.strIsNotEmpty(params.getSimp())) {
            bank.setBank(params.getSimp());
        }
        Long icon = params.getIcon();
        if (icon != null && icon > 0) {
            bank.setIcon(icon);
        }
        Integer state = params.getState();
        if (state != null) {
            if (state > 0) {
                //已删除
                bank.setDeleted(1);
            } else {
                bank.setDeleted(0);
            }
        }

        Integer sort = params.getSort();
        if (sort != null && sort > -1) {
            bank.setSort(sort);
        }
        int res = oaDBankMapper.updateById(bank);
        if (res > 0) {
            return Rv.wrap();
        }
        return Rv.error(EErrorCode.applyAmountOperateFailure);
    }

    /**
     * 获取支持银行信息
     *
     * @return List
     */
    @Override
    public Rv getBank() {

        List<Map<String, Object>> mapList = oaDBindBankCardMapper.getBank();

        for (Map<String, Object> map : mapList) {

            log.info((Long) map.get("icon") + "");

            String iconPath = ((Map<String, String>) (feginOssService.getFileUrl((Long) map.get("icon")).getData())).get("fileUrl").toString();

            map.put("icon", iconPath);
        }

        return Rv.wrap(mapList);
    }

    @Override
    public Rv getBank(Long id) {
        if (id == null || id < 0) {
            return Rv.error(EErrorCode.noData);
        }
        OaDBank data = oaDBankMapper.selectOne(Qw.create().eq(OaDBank.BANK_CARD_ID, id));
        return Rv.wrap(data);
    }

    /**
     * 获取司机姓名和身份证号
     *
     * @param driverId 司机ID
     * @return Map
     */
    @Override
    public Rv getDriverNameAndIDEntity(Long driverId) {
        Map<String, String> map = oaDWalletMapper.queryNameAndIDEntity(driverId);

        if (CheckUtil.mapIsEmpty(map)) {
            throw PangException.create(EErrorCode.unDriverInfo);
        }

        // 处理隐私信息
        String identity = map.get("identityNumber");
        String name = map.get("driverName");
        map.put("identityNumber", HiddenUtil.indntityBuffer(identity));
        map.put("driverName", HiddenUtil.nameBuffer(name));

        return Rv.wrap(map);
    }

    /**
     * 司机绑定银行卡信息
     *
     * @param oaBankCardBindDTO 银行卡信息
     * @return Map
     */
    @Override
    public Rv bindBankCard(OaBankCardBindDTO oaBankCardBindDTO) {

        String tag = TokenUtil.getTag();

        if (CheckUtil.objIsEmpty(oaBankCardBindDTO.getDriverId())
                /*|| CheckUtil.objIsEmpty(oaBankCardBindDTO.getBank())*/
                /*|| CheckUtil.objIsEmpty(oaBankCardBindDTO.getCode())*/
                /*|| CheckUtil.objIsEmpty(oaBankCardBindDTO.getBankCardName())*/
                /*|| CheckUtil.objIsEmpty(oaBankCardBindDTO.getIssuingPlace())*/
                || CheckUtil.objIsEmpty(oaBankCardBindDTO.getBankCardNumber())
            /*|| CheckUtil.objIsEmpty(oaBankCardBindDTO.getBankCardPhone())*/
        ) {
            log.info(oaBankCardBindDTO.toString());
            throw PangException.create(EErrorCode.missBankCardNumber);
        }

        // 去除银行卡空格
        oaBankCardBindDTO.setBankCardNumber(oaBankCardBindDTO.getBankCardNumber().replaceAll(" ", ""));

        // 设置银行卡类型
        String card = null;

        // 验证银行卡是否存在
        Rv checkResult = feginCheckBankCardService.bankCardCheck(oaBankCardBindDTO.getBankCardNumber());

        // 开户行
        Long bankInt = 0L;

        if (checkResult.getCode() != 0) {
            throw PangException.create(EErrorCode.bankCardFiled);
        } else {
            Map<String, String> map = (Map<String, String>) checkResult.getData();
            // 银行卡类型
            card = map.get("card");

            if (!(ECardType.DC.getKey().equals(card))) {
                throw PangException.create(EErrorCode.DC_BIND);
            }

            // 银行
            String bankType = map.get("bankType");
            String bank = map.get("bank");

            List<Map<String, Object>> mapList = oaDBindBankCardMapper.getBank();

            // 匹配统计
            Integer count = 0;

            for (Map<String, Object> bankElement : mapList) {
                if (CheckUtil.objIsNotEmpty(bankElement.get("bank"))) {
                    if (bankElement.get("bank").equals(bank)) {
                        bankInt = (Long) bankElement.get("bankCardId");
                        break;
                    }
                    count++;
                }
            }

            if (count.equals(mapList.size())) {
                throw PangException.create(EErrorCode.NOT_BANK_FILED);
            }

        }

        // 验证码key
        /* String smsCodeKey = CommonConstant.REDIS_SMS_LIMIT + "_" + TokenUtil.getTag() + "_" + TokenUtil.getUserType() + "_" + oaBankCardBindDTO.getBankCardPhone();

        if(!Objects.equals(oaBankCardBindDTO.getCode(),(String) redisTemplate.opsForValue().get(smsCodeKey))){
            throw PangException.create(EErrorCode.verifCodeError);
        }*/

        OaDBindBankCard oaDBindBankCard = BeanUtil.copyProperties(oaBankCardBindDTO, OaDBindBankCard.class);

        log.info(oaDBindBankCard.toString());

        Long bankCardId = IDUtil.nextId();
        oaDBindBankCard.setBankCardId(bankCardId);

        // 检索是否存在已绑定银行卡
        QueryWrapper<OaDBindBankCard> checkExistBankCardWrapper = new QueryWrapper<>();
        checkExistBankCardWrapper.eq(OaDBindBankCard.DRIVER_ID, oaBankCardBindDTO.getDriverId());
        Integer existCount = oaDBindBankCardMapper.selectCount(checkExistBankCardWrapper);

        if (existCount == 0) {
            // 如果没有绑定银行卡，设置使用此卡为主卡
            // 现在不需要了(暂时先留着)
            /// oaDBindBankCard.setIsUsed(BankCardConstant.IS_USED);
        } else {

            // 检查此银行卡是否已经绑定过--->查看 使用银行卡状态
            checkExistBankCardWrapper.eq(OaDBindBankCard.BANK_CARD_NUMBER, oaBankCardBindDTO.getBankCardNumber());
            checkExistBankCardWrapper.eq(OaDBindBankCard.IS_USED,BankCardConstant.IS_USED);//使用银行卡状态
            checkExistBankCardWrapper.select(OaDBindBankCard.BANK_CARD_ID, OaDBindBankCard.VERSION);
            OaDBindBankCard oaDBindBankCardVersion = oaDBindBankCardMapper.selectOne(checkExistBankCardWrapper);

            if (CheckUtil.objIsNotEmpty(oaDBindBankCardVersion)) {
                // 有-->清除所有卡绑定
                this.switchBankCard(oaBankCardBindDTO.getDriverId(), null);
                // 设置卡
                oaDBindBankCardVersion.setIssuingPlace(oaBankCardBindDTO.getIssuingPlace());
                oaDBindBankCardVersion.setIsUsed(BankCardConstant.IS_USED);

                Integer result = oaDBindBankCardMapper.updateById(oaDBindBankCardVersion);
                //Integer result = oaDBindBankCardMapper.insert(newOaDBindBankCard);
                Map<String, String> results = new HashMap<>(1);
                results.put("isSuccess", result.toString());

                // 发送消息
                OaSysMessage oaSysMessage = new OaSysMessage();
                oaSysMessage.setPushCode("bankCardBind");
                oaSysMessage.setDriverId(oaBankCardBindDTO.getDriverId());
                oaSysMessage.setTag(tag);
                oaSysMessage.setMessageType(2);

                if (result > 0) {
                    oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_BIND_SUCCESS + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
                } else {
                    oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_BIND_FILED + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
                }

                feignMessageService.saveMessage(oaSysMessage);

                return Rv.wrap(results);
            }

        }

        Map<String, String> map = oaDWalletMapper.queryNameAndIDEntity(oaBankCardBindDTO.getDriverId());

        if (CheckUtil.mapIsEmpty(map)) {
            throw PangException.create(EErrorCode.authIdentityErr);
        }

        // 若未绑定过此卡，则鉴权银行卡信息是否匹配
        String identity = map.get("identityNumber");
        String name = map.get("driverName");

        //验证银行卡三要素鉴权
        Rv rv = feginCheckBankCardService.bankCardAuthentication(oaBankCardBindDTO.getDriverId(), oaBankCardBindDTO.getBankCardNumber(), identity, name, tag);
        if (BankCardConstant.LHY_SUCCESS == rv.getCode()) {
            Map<String, Object> JSONMap = (Map<String, Object>) rv.getData();
            if (BankCardConstant.HTTP_SUCCESS.equals(Integer.parseInt(JSONMap.get("resultCode").toString()))) {
                Integer resultStatus = Integer.parseInt(JSONMap.get("result").toString());
                if (BankCardConstant.ATYPISM.equals(resultStatus)) {
                    throw PangException.create(EErrorCode.BANK_CARD_CHECK_ATYPISM);
                }
                if (BankCardConstant.UNCERTIFIED.equals(resultStatus)) {
                    throw PangException.create(EErrorCode.BANK_CARD_CHECK_UNCERTIFIED);
                }
                if (BankCardConstant.CANCELLED.equals(resultStatus)) {
                    throw PangException.create(EErrorCode.BANK_CARD_CHECK_CANCELLED);
                }
            } else {
                String message = JSONMap.get("message").toString();
                if (("Invalid id_number").equals(message) || ("入参错误，身份证号[idcard]格式错误").equals(message)) {
                    throw PangException.create(EErrorCode.INVALID_ID_NUMBER);
                } else {
                    throw PangException.create(EErrorCode.BANK_CARD_CHECK_ATYPISM, message, JSONMap.get("resultCode"));
                }
            }
        } else {
            throw PangException.create(EErrorCode.BANK_CARD_CHECK_SERVER_FILED);
        }

        // 清除所有银行卡绑定
        try {
            Integer clearResult = (Integer) (this.switchBankCard(oaBankCardBindDTO.getDriverId(), null).getData());
            if (clearResult < 0) {
                throw PangException.create(EErrorCode.bankCardBindFiled);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            throw PangException.create(EErrorCode.bankCardBindFiled);
        }

        // 目前的需求是谁来就把谁设置成主卡
        oaDBindBankCard.setIsUsed(BankCardConstant.IS_USED);

        // 设置司机姓名 （二次修改时，将用户姓名和身份证号一并查出）所以注释掉了这个查询
        // String driverName = oaDBindBankCardMapper.getDriverName(oaBankCardBindDTO.getDriverId());
        oaDBindBankCard.setBankCardName(name);

        if (bankInt == 0) {
            throw PangException.create(EErrorCode.NOT_BANK_FILED);
        } else {
            oaDBindBankCard.setBank(bankInt.toString());
        }

        if (CheckUtil.strIsNotEmpty(card)) {
            // 设置银行卡类型
            oaDBindBankCard.setCardType(card);
        }

        oaDBindBankCard.setTag(tag);
        oaDBindBankCard.setCreateUser(oaBankCardBindDTO.getDriverId());
        oaDBindBankCard.setCreateTime(new Date());
        oaDBindBankCard.setUpdateUser(oaBankCardBindDTO.getDriverId());

        Integer result = oaDBindBankCardMapper.insert(oaDBindBankCard);

        log.info("添加结果，{}", result);

        Map<String, String> results = new HashMap<>(1);
        results.put("isSuccess", result.toString());

        OaSysMessage oaSysMessage = new OaSysMessage();
        oaSysMessage.setPushCode("bankCardBind");
        oaSysMessage.setDriverId(oaBankCardBindDTO.getDriverId());
        oaSysMessage.setTag(tag);
        oaSysMessage.setMessageType(2);
        PushInfoEntity pushInfoEntity = new PushInfoEntity();
        // 设置标识
        pushInfoEntity.setPushCode("bankCardBind");
        // 设置别名
        List<String> strArr = new ArrayList<>();
        strArr.add(oaBankCardBindDTO.getBankCardPhone());
        pushInfoEntity.setPhones(strArr);
        // 设置推送内容
        if (result > 0) {
            oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_BIND_SUCCESS + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
            pushInfoEntity.setContent(MessageConstant.BANK_CARD_BIND_SUCCESS + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
        } else {
            oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_BIND_FILED + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
            pushInfoEntity.setContent(MessageConstant.BANK_CARD_BIND_FILED + oaBankCardBindDTO.getBankCardNumber().substring(oaBankCardBindDTO.getBankCardNumber().length() - 4));
        }
        // 设置扩展参数
        pushInfoEntity.setExtras(new JSONObject());
        pushInfoEntity.setClientId(TokenUtil.getClientId());
        pushInfoEntity.setTag(tag);
        feignMessageService.saveMessage(oaSysMessage);
        feignJPushService.pushMessage(pushInfoEntity);

        return Rv.wrap(results);
    }

    /**
     * 司机修改银行卡开户行
     *
     * @param oaDBindBankCard
     * @return
     */
    @Override
    public Rv updateBankBatch(OaDBindBankCard oaDBindBankCard) {

        Date now = new Date();
        Long driverId = TokenUtil.getTokenUserId();

        QueryWrapper<OaDBindBankCard> oaDBindBankCardQueryWrapper = new QueryWrapper<>();
        if (CheckUtil.objIsNotEmpty(oaDBindBankCard.getBankCardId())) {
            oaDBindBankCardQueryWrapper.eq(OaDBindBankCard.BANK_CARD_ID, oaDBindBankCard.getBankCardId());
        }
        oaDBindBankCardQueryWrapper.eq(OaDBindBankCard.DRIVER_ID, driverId);
        oaDBindBankCardQueryWrapper.eq(OaDBindBankCard.IS_USED, BankCardConstant.IS_USED);
        oaDBindBankCardQueryWrapper.select(OaDBindBankCard.VERSION, OaDBindBankCard.BANK_CARD_ID);

        OaDBindBankCard oaDBindBankCardVersion = oaDBindBankCardMapper.selectOne(oaDBindBankCardQueryWrapper);

        oaDBindBankCardVersion.setIssuingPlace(oaDBindBankCard.getIssuingPlace());

        Integer result = 0;

        try {
            result = oaDBindBankCardMapper.updateById(oaDBindBankCardVersion);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw PangException.create(EErrorCode.noData);
        }

        Map<String, Integer> resultMap = new HashMap<>(1);
        resultMap.put("isSuccess", result);

        if (result > 0) {
            return Rv.wrap(EErrorCode.success, EErrorCode.updateWalletSuccess.getDesc(), resultMap);
        } else {
            return Rv.wrap(EErrorCode.success, EErrorCode.updateWalletFailure.getDesc(), resultMap);
        }
    }

    /**
     * 检查司机银行卡绑定状态
     *
     * @param driverId 司机Id
     * @return Map
     */
    @Override
    public Rv checkBankCardBindStatus(Long driverId) {

        Integer score = null;

        try {
            QueryWrapper<OaDBindBankCard> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(OaDBindBankCard.DRIVER_ID,driverId);
            //score = oaDBindBankCardMapper.checkBankCardBindStatus(driverId);
            score = oaDBindBankCardMapper.selectCount(queryWrapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw PangException.create(EErrorCode.bindBankCardCheckField);
        }

        if (CheckUtil.objIsEmpty(score)) {
            log.error(EErrorCode.notFoundYourIdentity.toChString());
            throw PangException.create(EErrorCode.notFoundYourIdentity);
        }

        Map<String, String> resultMap = new HashMap<>(1);

        resultMap.put("score", score.toString());

        return Rv.wrap(resultMap);
    }

    /**
     * 司机切换银行卡 （现在是清除所有卡绑定）
     *
     * @param driverId 司机ID
     * @param cardId   银行卡ID
     * @return Map
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Rv switchBankCard(Long driverId, Long cardId) {

        OaDBindBankCard oaDBindBankCard = new OaDBindBankCard();

        QueryWrapper<OaDBindBankCard> updateWrapper = new QueryWrapper<>();
        updateWrapper.eq(OaDBindBankCard.DRIVER_ID, driverId);

        oaDBindBankCard.setIsUsed(BankCardConstant.IS_NOT_USED);

        // 清除所有银行卡使用
        Integer clearResult = oaDBindBankCardMapper.update(oaDBindBankCard, updateWrapper);

        // 暂时不适用重新绑定银行卡
        /*if (clearResult > 0) {

            oaDBindBankCard.setBankCardId(cardId);
            oaDBindBankCard.setIsUsed(BankCardConstant.IS_USED);

            // 重新绑定银行卡
            Integer bindResult = oaDBindBankCardMapper.updateById(oaDBindBankCard);

            if (bindResult > 0) {

                Map<String, String> result = new HashMap<>(1);
                result.put("isSuccess", bindResult.toString());

                return Rv.wrap(result);
            }

        }*/

        return Rv.wrap(clearResult);

        // throw PangException.create(EErrorCode.bindField);
    }

    /**
     * 司机查询已绑定银行卡列表
     *
     * @param driverId 司机ID
     * @return List
     */
    @Override
    public Rv driverBindBankCardList(Long driverId) {

        //List<DriverBindBankCardVO> driverBindBankCardVOList = null;
        List<OaDBindBankCard> driverBindBankCardVOList = null;
        try {
            QueryWrapper<OaDBindBankCard> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq(OaDBindBankCard.DRIVER_ID,driverId);
            //driverBindBankCardVOList = oaDBindBankCardMapper.driverBindBankCardList(driverId);
            driverBindBankCardVOList = oaDBindBankCardMapper.selectList(queryWrapper);
            if (CheckUtil.objIsNotEmpty(driverBindBankCardVOList)) {
                return Rv.wrap(driverBindBankCardVOList);
            } else {
                throw PangException.create(EErrorCode.unBankCard);
            }

        } catch (Exception e) {
            log.error(e.getMessage());
            throw PangException.create(EErrorCode.unBankCard);
        }

    }

    /**
     * 我的银行卡
     *
     * @return
     */
    @Override
    public Rv myCard() {

        //List<Map<String, String>> mapList = oaDBindBankCardMapper.myCard(TokenUtil.getTokenUserId());
        List<Map<String, String>> mapList = new ArrayList<>() ;
        Map<String, String> cardMap = new HashMap<>();
        //查询司机绑定银行卡信息
        QueryWrapper<OaDBindBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaDBindBankCard.DRIVER_ID,TokenUtil.getTokenUserId());
        List<OaDBindBankCard> driverBindBankCardVOList = oaDBindBankCardMapper.selectList(queryWrapper);
        log.info("查看我的银行卡");
        if (CheckUtil.objIsEmpty(driverBindBankCardVOList)) {
            throw PangException.create(EErrorCode.unBankCard);
        }
        for (OaDBindBankCard info : driverBindBankCardVOList){
            cardMap.put("bankId",info.getBankCardId().toString());
            cardMap.put("bankNumber",info.getBankCardNumber());
            cardMap.put("issuingPlace",info.getIssuingPlace());
        }
        //查询银行信息
        QueryWrapper<OaDBank> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq(OaDBank.BANK_CARD_ID,driverBindBankCardVOList.get(0).getBank());
        List<OaDBank> oaDBankList = oaDBankMapper.selectList(queryWrapper2);
        if (CheckUtil.objIsEmpty(oaDBankList)) {
            throw PangException.create(EErrorCode.unBankCard);
        }
        for (OaDBank info:oaDBankList) {
            cardMap.put("bankIcon",info.getIcon().toString());
            cardMap.put("bankStr",info.getBank());
            cardMap.put("bankgroundBankId",info.getValue());
        }
        //添加到map
        mapList.add(cardMap);
        if (CheckUtil.objIsEmpty(mapList)) {
            throw PangException.create(EErrorCode.unBankCard);
        }

        for (Map map : mapList) {
            map.put("bankIcon", ((Map<String, String>) (feginOssService.getFileUrl(Long.parseLong((String) map.get("bankIcon"))).getData())).get("fileUrl").toString());
            //map.put("bankgroundBank", ((Map<String, String>) (feginOssService.getFileUrl((Long) map.get("bankgroundBankId")).getData())).get("fileUrl").toString());
            String bankNumber = map.get("bankNumber").toString();
            map.put("bankNumber", "**** **** " + bankNumber.substring(bankNumber.length() - 4));
            map.put("bankId", map.get("bankId").toString());
            map.put("cardType", "储蓄卡");
        }
        return Rv.wrap(mapList);
    }

    /**
     * 解绑银行卡
     */
    @Override
    public Rv untieBindBank(Long bankId) {

        Long driverId = TokenUtil.getTokenUserId();

        QueryWrapper<OaDBindBankCard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(OaDBindBankCard.BANK_CARD_ID, bankId);
        queryWrapper.select(OaDBindBankCard.VERSION);

        OaDBindBankCard oaDBindBankCard = oaDBindBankCardMapper.selectOne(queryWrapper);

        if (CheckUtil.objIsEmpty(oaDBindBankCard)) {
            throw PangException.create(EErrorCode.unBankCard);
        }

        oaDBindBankCard.setBankCardId(bankId);
        // 设置不使用
        oaDBindBankCard.setIsUsed(0);

        Integer result = oaDBindBankCardMapper.updateById(oaDBindBankCard);

        Map<String, Integer> map = new HashMap<>(1);
        map.put("isSuccess", result);

        // 发送消息
        OaSysMessage oaSysMessage = new OaSysMessage();
        oaSysMessage.setPushCode("bankCardBind");
        oaSysMessage.setDriverId(driverId);
        oaSysMessage.setMessageType(0);
        if (result > 0) {
            oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_NOT_BIND_SUCCESS);
        } else {
            oaSysMessage.setMessageContent(MessageConstant.BANK_CARD_NOT_BIND_FILED);
        }

        feignMessageService.saveMessage(oaSysMessage);

        return Rv.wrap(map);

    }

}
